/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.*;
import entities.*;
import java.util.ArrayList;

/**
 *
 * @author lion
 */
public class CourseDao {

    private Connection conn = null;
    private Statement stm = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;
    MysqlHelper mysql = new MysqlHelper();

    public void addCourse(Course course) throws Exception {
        try {
            conn = mysql.GetConnection();
            ps = conn.prepareStatement(
                    "INSERT INTO fl_course VALUES(DEFAULT,?,?,?)");
            ps.setString(1, course.getCourseName());
            ps.setFloat(2, course.getFee());
            ps.setString(3, course.getPrerequisite());
            ps.executeUpdate();
        } catch (Exception e) {
            throw e;    
        }
    }
//
//    public void deleteCourse(Course course) throws Exception {
//        try {
//            conn = mysql.GetConnection();
//            ps = conn.prepareStatement("DELETE FROM fl_course WHERE courseid=?");
//            ps.setInt(1, course.getCourseid());
//        } catch (Exception e) {
//            throw e;
//        }
//    }
//
//    public void editCourse(Course course) throws Exception {
//        try {
//            conn = mysql.GetConnection();
//            mysql.ExecuteUpdate("UPDATE fl_course SET course_name='" + course.getCourseName()
//                    + "' WHERE courseid=" + course.getCourseid());
//            mysql.ExecuteUpdate("UPDATE fl_course SET Fee=" + course.getFee()
//                    + " WHERE courseid=" + course.getCourseid());
//            mysql.ExecuteUpdate("UPDATE fl_course SET Prerequisite='" + course.getPrerequisite()
//                    + "' WHERE courseid=" + course.getCourseid());
//        } catch (Exception e) {
//            throw e;
//        }
//    }

    public ArrayList<Course> getListCourse() throws Exception {
        ArrayList<Course> listCourse = new ArrayList<Course>();
        conn = mysql.GetConnection();
        rs = mysql.ExecuteReader("SELECT * FROM fl_course");
        while (rs.next()) {            
            Course course = new Course();
            course.setCourseid(rs.getInt("courseid"));
            course.setCourseName(rs.getString("course_name"));
            course.setFee(rs.getFloat("Fee"));
            course.setPrerequisite(rs.getString("Prerequisite"));
            listCourse.add(course);
        }
        return listCourse;
    }
}
